//
//  LzwIndexingVC.m
//  CoJellyCartoon
//
//  Created by 刘志武 on 16/4/5.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "LzwIndexingVC.h"
#import "LzwIndexCollectCell.h"
#import "LzwIndexDetailVc.h"

@interface LzwIndexingVC ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *mycollectView;

@end

@implementation LzwIndexingVC




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"索引";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 20, 20);
    [button setImage:[[UIImage imageNamed:@"L20.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(LeftButtonAxtion:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = barItem;
    
    
    //初始化数组
    self.arrayIndex = [NSMutableArray array];
    
    //创建collectView
    [self creatCollectIndex];
    
}

- (void)creatCollectIndex{
    
    UICollectionViewFlowLayout *myLayout = [[UICollectionViewFlowLayout alloc]init];
    
    myLayout.itemSize = CGSizeMake(120, 120);
    
    myLayout.sectionInset = UIEdgeInsetsMake(5, 3, 5, 5);
    
    myLayout.minimumInteritemSpacing = 3;
    myLayout.minimumLineSpacing = 5;
    
    self.mycollectView = ({
        UICollectionView *mycollect = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:myLayout];
        mycollect.backgroundColor =[UIColor whiteColor];
        mycollect.delegate = self;
        mycollect.dataSource = self;
        
        [self.view addSubview:mycollect];
        mycollect;
    });
    
    
    [self.mycollectView registerClass:[LzwIndexCollectCell class] forCellWithReuseIdentifier:@"cell"];
    
    //请求数据
    [GiFHUD show];
    [GiFHUD setGifWithImageName:@"hold.gif"];
    
    [self getIndexData];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //(@"... = %ld", self.arrayIndex.count);
    return self.arrayIndex.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwIndexCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.modelIndex = _arrayIndex[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwIndexDetailVc *detailVc = [[LzwIndexDetailVc alloc]init];
    
    
    LzwIndexModle *model = _arrayIndex[indexPath.row];
    detailVc.tagId = model.tag_id;
    detailVc.title = model.tag_name;
    [self.navigationController pushViewController:detailVc animated:YES];
}

- (void)getIndexData{
    
    NSString *url = @"http://bangumi.bilibili.com/api/tags?_device=android&_hwid=e647d7e136a2c2a9&_ulv=10000&access_key=ac2771d488a63b7c3f8ccf926d8fe761&appkey=c1b107428d337928&build=414000&page=1&pagesize=30&platform=android&ts=1459345611000&sign=e90aaba785398cde38ea86574f27cc15";
    
    [WZAFNetTool GetNetWithURL:url WithBody:nil headfile:nil response:WZJSON success:^(id reuslt) {
       
        NSMutableArray *array = [reuslt objectForKey:@"result"];
        
        for (NSMutableDictionary *dict in array) {
            
            LzwIndexModle *modelIndex = [[LzwIndexModle alloc]init];
            
            [modelIndex setValuesForKeysWithDictionary:dict];
            
            [self.arrayIndex addObject:modelIndex];
            
        }
        
        
        [self.mycollectView reloadData];
        
        [GiFHUD dismiss];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}


- (void)LeftButtonAxtion:(UIButton *)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
